package com.haoshuang.zhouzhoubang.ui.activity

import android.os.Bundle
import com.haoshuang.zhouzhoubang.databinding.ActivityShouKuanBinding
import com.haoshuang.zhouzhoubang.model.event.ShouKuanPaySuccessEvent
import com.haoshuang.zhouzhoubang.ui.dialog.ConfirmDialog
import com.haoshuang.zhouzhoubang.ui.vm.ShouKuanViewModel
import com.jeremyliao.liveeventbus.LiveEventBus
import com.lxj.xpopup.XPopup
import com.mufeng.libs.base.BaseVMActivity
import com.mufeng.libs.state.observeState
import com.mufeng.libs.utils.ktx.getString
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.click
import com.mufeng.libs.utils.view.clickWithTrigger
import com.mufeng.libs.utils.view.load


/**
 *
 */
class ShouKuanActivity : BaseVMActivity<ShouKuanViewModel, ActivityShouKuanBinding>() {

    // 订单id
    private var order_id = ""

    // 订单服务名称
    private var serviceName = ""


    override fun initIntent() {
        order_id = intent.getString("team_id")
        serviceName = intent.getString("serviceName")
    }

    override fun initView(savedInstanceState: Bundle?) {
        binding.appBar.ivBack.click { finish() }
        binding.appBar.tvTitle.text = "收款"

        binding.tvPayError.clickWithTrigger {
            // 支付失败
        }

        binding.tvPaySuccess.clickWithTrigger {
            // 支付成功
            LiveEventBus.get(ShouKuanPaySuccessEvent::class.java)
                .post(ShouKuanPaySuccessEvent(order_id))
            finish()
        }

        XPopup.Builder(mActivity)
            .asCustom(
                ConfirmDialog(
                    context = mActivity,
                    title = "温馨提示",
                    content = "禁止私下收款交易，若经核实私下交易违规者将按违规处理封闭账号",
                    cancelStr = "",
                    confirmStr = "知道了",
                    isHideCancel = true,
                    onCancel = {},
                    onConfirm = {})
            )
            .show()

        binding.tvServiceName.text = serviceName
        binding.tvOrderNumber.text = order_id
    }

    override fun initData() {

        vm.getLogistics("order_id" to order_id)
    }

    override fun startObserve() {
        vm.getLogistics.observeState(this) {
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                toast(msg)
            }
            onSuccess {
                dismissLoading()
                if (it != null) {
                    binding.apply {
                        ivImage.load(it.pay_code_url)
                    }

                }

            }
        }

    }

}